package com.qfedu.controller;

import com.qfedu.entity.Comment;
import com.qfedu.entity.ResponseData;
import com.qfedu.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @RequestMapping("/songList/detail")
    public List<Comment> getAllCommentOfSongList(int songListId) {

        List<Comment> result = commentService.getAllCommentOfSongList(songListId);
        return result;
    }

    @RequestMapping("/song/detail")
    public List<Comment> getAllCommentOfSong(int songId) {

        List<Comment> result = commentService.getAllCommentOfSong(songId);
        return result;
    }

    @RequestMapping("/add")
    public ResponseData<String> setComment(Comment comment) {
        ResponseData<String> responseData = new ResponseData<>();
        comment.setCreateTime(new Date());
        boolean result = commentService.add(comment);
        if (result) {
            responseData.setCode(1);
            responseData.setMsg("添加成功");
        } else {
            responseData.setCode(0);
            responseData.setMsg("添加失败");
        }
        return responseData;
    }

    @RequestMapping("/like")
    public ResponseData<String> setLike(Comment comment) {
        ResponseData<String> responseData = new ResponseData<>();
        boolean result = commentService.setLike(comment);
        if (result) {
            responseData.setCode(1);
            responseData.setMsg("点赞成功");
        }

        return responseData;
    }

    @RequestMapping("/update")
    public ResponseData<String> updateCommentMsg(Comment comment) {
        ResponseData<String> responseData = new ResponseData<>();
        System.out.println("comment = " + comment);
        comment.setCreateTime(new Date());
        boolean result = commentService.updateCommentMsg(comment);
        if (result) {
            responseData.setCode(1);
            responseData.setMsg("更新成功");
        }

        return responseData;
    }

    @RequestMapping("/delete")
    public ResponseData<String> deleteComment(Comment comment) {
        ResponseData<String> responseData = new ResponseData<>();
        boolean result = commentService.deleteComment(comment);
        if (result) {
            responseData.setCode(1);
            responseData.setMsg("更新成功");
        }

        return responseData;
    }
}
